<?php

class UserAction extends BaseAction
{

    public function shareFeed(){
        // 获取传入的值
        //body: 我的转发2
        $post = $_POST;
        $post['type'] = 'feed';
        $post['app_name'] = 'w3g';
        $post['curtable'] = 'feed';
        //$post['feed_title'] = '';
        if(empty($post['body'])){
            $return = array( 'status' => 100, 'data'   => '内容不能为空', );
            exit(json_encode($return));
        }
        if(empty($post['sid']) ){
            $return = array( 'status' => 100, 'data'   => '转发的id不能为空', );
            exit(json_encode($return));
        }
        $post['curid'] = $post['sid'];
        $uid = $this->uid;
        if (!$uid) {
            $this->ajaxReturn([], '用户信息失败', 100);
        }
        // 安全过滤
        foreach ($post as $key => $val) {
            $post[$key] = t($post[$key]);
        }
        // 过滤内容值
        // $post['body'] = filter_keyword($post['body']);
        $filterBodyStatus = filter_words($post['body']);
        if (!$filterBodyStatus['status']) {
            $return = array(
                'status' => 0,
                'data'   => $filterBodyStatus['data'],
            );
            exit(json_encode($return));
        }
        $post['body'] = $filterBodyStatus['data'];

        // 判断资源是否删除
        if (empty($post['curid'])) {
            $map['feed_id'] = intval($post['sid']);
        } else {
            $map['feed_id'] = intval($post['curid']);
        }
        $map['is_del'] = 0;
        $isExist = model('Feed')->where($map)->count();
        if ($isExist == 0) {
            $return['status'] = 100;
            $return['data'] = '内容已被删除，转发失败';
            exit(json_encode($return));
        }else{
            $getTopicIdRs = D('feed_data')->where([  'feed_id'=> $post['sid']])->find();
            $getTopicId = $getTopicIdRs ? $getTopicIdRs['topic_id'] : '';
            $post['topic_id'] = $getTopicId;
        }

        // 进行分享操作
        $return = model('Share')->shareFeed($post, 'share');
        if ($return['status'] == 1) {
            //$app_name = $post['app_name'];
            //$this->assign($return['data']);
            // 分享配置
            //$weiboSet = model('Xdata')->get('admin_Config:feed');
            //$this->assign('weibo_premission', $weiboSet['weibo_premission']);
            $return['feed_id'] = $return['data']['feed_id'];
            $return['data'] = '';//$this->fetch('PostFeed');
        }

        if ($post['comment'] == '1') {
        }
        exit(json_encode($return));
    }
    public function getFeedInfo()
    {
        $uid = $this->uid;
        $feed_id = intval($_GET['feed_id']);
        $read_status = isset($_GET['read']); //是否执行已读状态
        $feedInfo = [];
        if (!$uid) {
            $this->ajaxReturn([], '用户信息失败', 100);
        } else {
            $feedInfo = model('Feed')->getFeedInfo($feed_id);
            if($read_status){
                model('MypushNotice')->where( ['feed_id' => $feed_id ,'to_uid'=>$uid ] )->save(['status'=>1]);
            }
            if ($feedInfo) {
                $this->doContentLog('content', 5 , 0 , $feed_id);
                $feedInfo['publish_time'] = date('Y-m-d H:i', $feedInfo['publish_time']);
                unset($feedInfo['user_group'], $feedInfo['user_gicon']);
                $where['row_id'] = $feed_id;
                $where['table'] = 'feed';
                $where['type'] = 1;
                $feedInfo['comment_list'] = model('Comment')->getCommentList($where);
                $my_digg_rs = D('feed_digg')->where(['feed_id'=> $feed_id, 'uid'=>$uid])->find();
                $feedInfo['my_digg'] = empty($my_digg_rs) ? 0: 1;
                $this->ajaxReturn($feedInfo, 'success' , 200);
            } else {
                $this->ajaxReturn([], 'error', 100);
            }
        }

    }

    public function delfeed()
    {
        $return = array(
            'status' => 200,
        );

        $feed_id = $_GET['feed_id'];
        if (empty($feed_id)) {
            $return = array(
                'status' => 100,
                'data' => 'feedid参数错误',
            );
            exit(json_encode($return, JSON_UNESCAPED_UNICODE));
        }
        $rs = model('Feed')->getFeedInfo($feed_id, true);
        $uid = $this->uid;
        if ($rs['uid'] != $uid) {
            $return = array(
                'status' => 100,
                'data' => '不是该用户的分享',
            );
            exit(json_encode($return, JSON_UNESCAPED_UNICODE));
        } else {
            $rs = model('Feed')->doEditFeed($feed_id, 'delFeed');
        }
        $return['data'] = 'success';
        $return['rs'] = $rs;
        exit(json_encode($return, JSON_UNESCAPED_UNICODE));

    }


    public function editfeed()
    {

        $return = array(
            'status' => 200,
        );
        $feed_id = $_POST['feed_id'];
        if (empty($feed_id)) {
            $return = array(
                'status' => 0,
                'data' => 'feedid参数错误',
            );
            exit(json_encode($return, JSON_UNESCAPED_UNICODE));
        }
        $uid = $this->uid;
        // 用户发送内容
        $d['content'] = isset($_POST['content']) ? h($_POST['content']) : '';
        $filterContentStatus = filter_words($d['content']);
        if (!$filterContentStatus['status']) {
            exit(json_encode(array(
                'status' => 0,
                'data' => $filterContentStatus['data'],
            ), JSON_UNESCAPED_UNICODE));
        }
        $d['content'] = $filterContentStatus['data'];

        if ($_POST['topic_id'] == '') {
            $return = array(
                'status' => 0,
                'data' => '请选择话题',
            );
            exit(json_encode($return, JSON_UNESCAPED_UNICODE));
        }
        $topic_id = $_POST['topic_id'];
        $topic_res = model('FeedTopic')->where(['topic_id' => t($_POST['topic_id'])])->find();
        if (!$topic_res) {
            $return = array(
                'status' => 0,
                'data' => '请选择话题',
            );
            exit(json_encode($return, JSON_UNESCAPED_UNICODE));
        }

        // 原始数据内容
        $filterBodyStatus = filter_words($_POST['body']);
        if (!$filterBodyStatus['status']) {
            $return = array(
                'status' => 0,
                'data' => $filterBodyStatus['data'],
            );
            exit(json_encode($return, JSON_UNESCAPED_UNICODE));
        }
        $d['body'] = $filterBodyStatus['data'];
        $d['feed_title'] = t($_POST['feed_title']);
        // 安全过滤
        foreach ($_POST as $key => $val) {
            $_POST[$key] = t($_POST[$key]);
        }
        $d['source_url'] = urldecode($_POST['source_url']); // 应用分享到分享，原资源链接
        // 滤掉话题两端的空白

        //$d['body'] = preg_replace("/#[\s]*([^#^\s][^#]*[^#^\s])[\s]*#/is", '#' . trim('${1}') . '#', $d['body']);

        // $numbers = array(41624,41625,41626,41627,41628,41629);
        // shuffle($numbers);
        // // 附件信息
        // if($_POST ['attach_id']==''&&$_POST ['channel_id']!=''){
        // 	$_POST ['attach_id'] = '|'.$numbers[0].'|'.$numbers[1].'|'.$numbers[2].'|';
        // 	$_POST ['type'] = 'postimage';
        // }

        //$d['attach_id'] = t($_POST['attach_id']);

        if ($_POST['feed_title']) {
            $d['feed_title'] = t($_POST['feed_title']);
        }
        if ($_POST['feed_picture']) {
            $type = 'postimage';
            $d['feed_picture'] = $d['attach_id'] = $_POST['feed_picture'];
        } else {
            // 发送分享的类型
            $type = 'post';
        }
        if (!empty($d['attach_id'])) {
            $d['attach_id'] = explode('|', $d['attach_id']);

            array_map('intval', $d['attach_id']);
        }
        $d['topic_id'] = isset($_POST['topic_id']) ? intval($_POST['topic_id']) : 0;

        $return['uid'] = $uid;
        $return['feed_id'] = $feed_id;
        // 附件信息
        // 所属应用名称
        $app = 'w3g'; // 当前动态产生所属的应用
        if (!$data = model('Feed')->editfeed($feed_id, $d)) {
            $return = array(
                'status' => 100,
                'data' => model('Feed')->getError(),
            );
            exit(json_encode($return));
        }
        exit(json_encode($return));
    }

    /**
     * 发布分享操作，用于AJAX.
     *
     * @return json 发布分享后的结果信息JSON数据
     */
    public function postfeed()
    {
        // 返回数据格式
        $return = array('status' => 200);
        $uid = $this->uid;
        // 用户发送内容
        $d['content'] = isset($_POST['content']) ? h($_POST['content']) : '';
        $filterContentStatus = filter_words($d['content']);
        if (!$filterContentStatus['status']) {
            exit(json_encode(array(
                'status' => 0,
                'data' => $filterContentStatus['data'],
            ), JSON_UNESCAPED_UNICODE));
        }
        $d['content'] = $filterContentStatus['data'];
        $topic_is_need = false ;
        $topic_config = model('Xdata')->get('admin_Config:topic');
        if($topic_config){
            $is_set_default = $topic_config['is_set_default'] ;  //手机社区内某一话题下点击发帖，发帖页面默认选择该话题
            $topic_is_need  =  ($topic_config['is_need_check'] && $topic_config['is_need_check'] == 1) ? true : false   ;   //手机社区内点击发帖，发帖页面必须强制选择一个话题才能发帖
        }

        if ( $topic_is_need && empty($_POST['topic_id'])) {
            $return = array(
                'status' => 0,
                'data' => '请选择话题',
            );
            exit(json_encode($return, JSON_UNESCAPED_UNICODE));
        }
        /*$topic_res = model('FeedTopic')->where(['topic_id' => t($_POST['topic_id'])])->find();
        if (!$topic_res) {
            $return = array(
                'status' => 0,
                'data' => '请选择话题',
            );
            exit(json_encode($return, JSON_UNESCAPED_UNICODE));
        }*/

        // 原始数据内容
        $filterBodyStatus = filter_words($_POST['body']);
        if (!$filterBodyStatus['status']) {
            $return = array(
                'status' => 0,
                'data' => $filterBodyStatus['data'],
            );
            exit(json_encode($return, JSON_UNESCAPED_UNICODE));
        }
        $d['body'] = $filterBodyStatus['data'];
        $d['feed_title'] = t($_POST['feed_title']);
        // 安全过滤
        foreach ($_POST as $key => $val) {
            $_POST[$key] = t($_POST[$key]);
        }
        $d['source_url'] = urldecode($_POST['source_url']); // 应用分享到分享，原资源链接
        // 滤掉话题两端的空白
        //$d['body'] = preg_replace("/#[\s]*([^#^\s][^#]*[^#^\s])[\s]*#/is", '#' . trim('${1}') . '#', $d['body']);

        if ($_POST['feed_title']) {
            $d['feed_title'] = t($_POST['feed_title']);
        }
        if ($_POST['feed_picture']) {
            $type = 'postimage';
            $d['feed_picture'] = $d['attach_id'] = $_POST['feed_picture'];
        } else {
            // 发送分享的类型
            $type = 'post';
        }
        if (!empty($d['attach_id'])) {
            $d['attach_id'] = explode('|', $d['attach_id']);

            array_map('intval', $d['attach_id']);
        }
        $d['topic_id'] = isset($_POST['topic_id']) ? intval($_POST['topic_id']) : 0;

        $return['uid'] = $uid;
        // 附件信息
        // 所属应用名称
        $app = isset($_POST['app_name']) ? t($_POST['app_name']) : 'w3g'; // 当前动态产生所属的应用

        if (!$data = model('Feed')->put($uid, $app, $type, $d)) {
            $return = array(
                'status' => 100,
                'data' => model('Feed')->getError(),
            );
            exit(json_encode($return));
        }
        $this->doContentLog('content', 1);
        // 发布邮件之后添加积分
        //model('Credit')->setUserCredit($uid, 'add_weibo');
        // 分享来源设置
        $data['from'] = 'w3g';
        //$this->assign($data);
        // 分享配置
        //$return['data'] = $this->fetch();
        // 分享ID
        $return['feed_id'] = $data['feed_id'];
        $return['is_audit'] = $data['is_audit'];
        // 添加话题
        //model('FeedTopic')->addTopic(html_entity_decode($d['body'], ENT_QUOTES, 'UTF-8'), $data['feed_id'], $type);
        // 更新用户最后发表的分享
        $last['last_feed_id'] = $data['feed_id'];
        $last['last_post_time'] = $_SERVER['REQUEST_TIME'];
        model('User')->where('uid=' . $uid)->save($last);

        // 绑定用户
        $bindUserChannel = D('Channel', 'channel')->getCategoryByUserBind($this->mid);

        exit(json_encode($return));
    }

    /**
     * 保存登录用户的头像设置操作.
     *
     * @return json 返回操作后的JSON信息数据
     */
    public function doSaveAvatar()
    {

        $dAvatar = model('Avatar');
        $dAvatar->init($this->mid); // 初始化Model用户id
        // 安全过滤
        $step = t($_GET['step']);
        if ('upload' == $step) {
            $result = $dAvatar->upload();
        } elseif ('save' == $step) {
            $result = $dAvatar->dosave();
        }
        model('User')->cleanCache($this->mid);
        $user_feeds = model('Feed')->where('uid=' . $this->mid)->field('feed_id')->findAll();
        if ($user_feeds) {
            $feed_ids = getSubByKey($user_feeds, 'feed_id');
            model('Feed')->cleanCache($feed_ids, $this->mid);
        }

        $this->ajaxReturn($result['data'], $result['info'], $result['status']);
    }

    public function my_article()
    {
        $uid = $this->uid;
        $rs = []; //
        $page_size = isset($_REQUEST['page_size']) && $_REQUEST['page_size'] ? $_REQUEST['page_size'] : 10;
        $p = isset($_REQUEST['p']) && $_REQUEST['p'] ? $_REQUEST['p'] : 1;
        $start_row = ($p-1)*$page_size;
        $map = ['uid' => $uid];
        if (!$uid) {
            $this->ajaxReturn([], '获取信息失败', 100);
        } else {
            $count = model('Feed')->where($map)->order('feed_id desc')->count();
            $rs = model('Feed')->where($map)->order('feed_id desc')->field('feed_id,is_audit,digg_count,comment_count,publish_time,`from`')->findPage(10,$count);
            //echo model('Feed')->getLastSql();
            foreach ($rs['data'] as $k => $v) {
                $picture_arr = [];
                if ($v) {
                    $rs_data = d('FeedData')->where(['feed_id' => $v['feed_id']])->find();

                    $rs['data'][$k]['content'] = ($rs_data['feed_content']);
                    if ($rs_data['feed_picture']) {
                        $picture = explode('|', $rs_data['feed_picture']);
                        if ($picture && count($picture) > 0) {
                            foreach ($picture as $kk => $vv) {
                                if ($vv) {
                                    $picture_arr[] = getImageUrlByAttachId($vv);
                                }
                            }
                        }
                    }
                    $rs['data'][$k]['topic_name'] = $rs_data['topic_name'];
                    $rs['data'][$k]['picture_url'] = $picture_arr;
                    $rs['data'][$k]['publish_time'] = date('Y-m-d H:i:s');
                    $rs['data'][$k]['title'] = $rs_data['feed_title'] ? $rs_data['feed_title'] : '';
                    $where = ['type' => 1, 'table' => 'feed', 'row_id' => $v['feed_id']];
                    $comment = model('Comment')->getCommentList($where);
                    $rs['data'][$k]['comment'] = !empty($comment) ? $comment : [];
                }
            }
        }
        unset($rs['html']);
        //$listData = model('Feed')->getList($map, $page_size);

        $rs_count = model('Feed')->where(['uid' => $uid])->count();
        $return['total'] = $rs_count;
        $user_info = getUserInfo($uid);
        unset($user_info['credit_info']);
        //$return['user_info'] = $user_info;
        //$return['list'] = $rs;
        $this->ajaxReturn($rs, 'success', 200);

    }

    //我的喜欢(分享)
    public function my_favor()
    {
        $uid = $this->uid;
        $rs = []; //
        if (!$uid) {
            $this->ajaxReturn([], '获取信息失败', 100);
        } else {
            $page_size = isset($_REQUEST['page_size']) && $_REQUEST['page_size'] ? $_REQUEST['page_size'] : 20;
            $rs = model('FeedDigg')->where(['uid' => $uid])->limit($page_size)->order('id desc')->select();

            foreach ($rs as $k => $v) {
                $picture_arr = [];
                if ($v) {
                    $rs_data = d('FeedData')->where(['feed_id' => $v['feed_id']])->find();
                    $rs_feed = d('Feed')->where(['feed_id' => $v['feed_id']])->find();
                    $rs[$k]['content'] = ($rs_data['feed_content']);
                    if ($rs_data['feed_picture']) {
                        $picture = explode('|', $rs_data['feed_picture']);
                        if ($picture && count($picture) > 0) {
                            foreach ($picture as $kk => $vv) {
                                if ($vv) {
                                    $picture_arr[] = getImageUrlByAttachId($vv);
                                }
                            }
                        }
                    }

                    $rs[$k]['picture_url'] = $picture_arr;
                    $rs[$k]['digg_count'] = $rs_feed['digg_count'] ? $rs_feed['digg_count'] : 0; //喜欢数量
                    $rs[$k]['comment_count'] = $rs_feed['comment_count'] ? $rs_feed['comment_count'] : 0; //评论数量
                    $rs[$k]['publish_time'] = date('Y-m-d H:i:s');
                    $rs[$k]['title'] = $rs_data['feed_title'] ? $rs_data['feed_title'] : '';
                    $rs[$k]['feed_title'] = $rs_data['feed_title'] ? $rs_data['feed_title'] : '';
                    $rs[$k]['feed_id']    = $v['feed_id'] ? $v['feed_id'] : '';
                    if ($rs_data['topic_id'] && empty($rs_data['topic_name'])) {
                        $res = model('FeedTopic')->where(['topic_id' => $rs_data['topic_id']])->find();
                        $rs[$k]['topic_name'] = $res['topic_name'] ? $res['topic_name'] : '';
                        $rs[$k]['topic_id'] = $res['topic_id'] ? $res['topic_id'] : '';
                    }else{
                        $rs[$k]['topic_name'] = $rs_data['topic_name'] ? $rs_data['topic_name'] : '';
                        $rs[$k]['topic_id']   = $rs_data['topic_id']   ? $rs_data['topic_id']   : '';
                    }
                    $rs[$k]['user_info'] = getUserInfo($rs_feed['uid']);
                    unset($rs[$k]['user_info']['user_group']) ;
                    unset($rs[$k]['user_info']['api_user_group']) ;
                    unset($rs[$k]['user_info']['group_icon_only']) ;
                    unset($rs[$k]['user_info']['credit_info']) ;
                }
            }
        }
        $rs_count = model('FeedDigg')->where(['uid' => $uid])->count();
        $return['total'] = $rs_count;
        //$user_info = getUserInfo($uid);
        //unset($user_info['credit_info']);
        //$return['user_info'] = $user_info;
        $return['list'] = $rs;
        $this->ajaxReturn($return, 'success', 200);
    }

    public function my_comment()
    {
        $uid = $this->uid;
        $rs = []; //
        $feed_id_arr = [];
        $page_size = isset($_REQUEST['page_size']) && $_REQUEST['page_size'] ? $_REQUEST['page_size'] : 10;
        if (!$uid) {
            $this->ajaxReturn([], '获取信息失败', 100);
        } else {
            $feed_id_arr = [];
            $comment_arr = []; //评论内容
            $comment_rs = model('Comment')->where(['uid' => $uid])->order('comment_id desc')->limit($page_size)->field('comment_id,content,digg_count,row_id,uid,`ctime`')->select();
            if ($comment_rs) {
                foreach ($comment_rs as $key => $val) {
                    //先去重
                    if ($val['row_id'] && (count($feed_id_arr) == 0 || !in_array($val['row_id'], $feed_id_arr))) {
                        $feed_id_arr[] = $val['row_id'];
                    }
                    $comment_arr[$val['row_id']][] = parse_html($val['content']); //分享的评论内容，数组格式
                }
                foreach ($feed_id_arr as $k => $v) {
                    $picture_arr = [];
                    if ($v) {
                        $rs_data = d('FeedData')->where(['feed_id' => $v])->find();
                        $rs[$k]['feed_id'] = $v;
                        $rs[$k]['content'] = parse_html( $rs_data['feed_content']);
                        if ($rs_data['feed_picture']) {
                            $picture = explode('|', $rs_data['feed_picture']);
                            if ($picture && count($picture) > 0) {
                                foreach ($picture as $kk => $vv) {
                                    if ($vv) {
                                        $picture_arr[] = getImageUrlByAttachId($vv);
                                    }
                                }
                            }
                        }
                        $rs_feed = model('Feed')->where(['feed_id' => $v])->field('digg_count,comment_count,publish_time,uid')->find();
                        $rs[$k]['comment_count'] = $rs_feed['comment_count'];
                        $rs[$k]['digg_count'] = $rs_feed['digg_count'];
                        $rs[$k]['picture_url'] = $picture_arr;
                        $rs[$k]['publish_time'] = date('Y-m-d H:i:s');
                        $rs[$k]['title'] = $rs_data['feed_title'] ? $rs_data['feed_title'] : '';
                        $rs[$k]['comment_content'] = $comment_arr[$v];
                        //话题显示
                        if ($rs_data['topic_id'] && empty($rs_data['topic_name'])) {
                            $res = model('FeedTopic')->where(['topic_id' => $rs_data['topic_id']])->find();
                            $rs[$k]['topic_name'] = $res['topic_name'] ? $res['topic_name'] : '';
                            $rs[$k]['topic_id'] = $res['topic_id'] ? $res['topic_id'] : '';
                        }else{
                            $rs[$k]['topic_name'] = $rs_data['topic_name'] ? $rs_data['topic_name'] : '';
                            $rs[$k]['topic_id'] = $rs_data['topic_id'] ? $rs_data['topic_id'] : 0;
                        }
                        $rs[$k]['user_info'] = getUserInfo($rs_feed['uid']);
                        unset($rs[$k]['user_info']['user_group']) ;
                        unset($rs[$k]['user_info']['api_user_group']) ;
                        unset($rs[$k]['user_info']['group_icon_only']) ;
                        unset($rs[$k]['user_info']['credit_info']) ;
                    }
                }
            }


        }
        $return['total'] = count($feed_id_arr);
        $return['list'] = $rs;
        $this->ajaxReturn($return, 'success', 200);
    }

    //我的消息 【关注了我，评论了我】
    public function my_message()
    {
        $uid = $this->uid;
        $feeds_arr = [];
        $return_2 = [];
        if ($uid) {
            //我的feed数组
            $message_digg_list = [];
            $feeds_rs = model('Feed')->where(['uid' => $uid])->select();
            if ($feeds_rs) {
                foreach ($feeds_rs as $feed) {
                    $feeds_arr[] = $feed['feed_id'];
                }
            }
            if (count($feeds_arr) > 0) {
                $where['feed_id'] = ['in', $feeds_arr];
                $didd_list = model('FeedDigg')->where($where)->order('id desc')->select();
                if ($didd_list) {
                    foreach ($didd_list as $v) {
                        $feed_data_rs = d('FeedData')->where(['feed_id' => $v['feed_id']])->find();
                        $message_digg_list = getUserInfo($v['uid'])['nickname'] . '点赞了分享' . $feed_data_rs['feed_title'];//
                        $return_4['user_info'] = getUserInfo($v['uid']);
                        $return_4['feed_title'] = $feed_data_rs['feed_title'];
                        $return_4['feed_id'] = $v['feed_id'];
                        $return_4['is_read'] = empty($v['is_read']) ? 0 : 1;
                        unset($return_4['user_info']['user_group']) ;
                        unset($return_4['user_info']['api_user_group']) ;
                        unset($return_4['user_info']['group_icon_only']) ;
                        unset($return_4['user_info']['credit_info']) ;
                        $return_1[] = $return_4;
                    }
                }
                $where2['row_id'] = ['in', $feeds_arr];
                $comment_list = model('Comment')->where($where2)->order('comment_id desc')->select();
                if ($comment_list) {
                    foreach ($comment_list as $vv) {
                        $feed_data_rs = d('FeedData')->where(['feed_id' => $vv['row_id']])->find();
                        //$message_commnet_list = getUserInfo($v['uid'])['nickname'] . '评论了分享' . $feed_data_rs['feed_title'] ;
                        $return_3['user_info'] = getUserInfo($v['uid']);
                        unset($return_3['user_info']['user_group']) ;
                        unset($return_3['user_info']['api_user_group']) ;
                        unset($return_3['user_info']['group_icon_only']) ;
                        unset($return_3['user_info']['credit_info']) ;
                        $return_3['feed_title'] = $feed_data_rs['feed_title'];
                        $return_3['feed_id']    = $feed_data_rs['feed_id'];
                        $return_3['comment_content']    = $vv['content'];
                        $return_3['is_read']    = empty($vv['is_read']) ? 0 : 1;
                        $return_2[]  = $return_3 ;
                    }

                }
            }


            $return['digg_list'] = $return_1;
            $return['comment_list'] = $return_2;
            $this->ajaxReturn($return, '', 200);

        } else {
            $return[] = '';
            $this->ajaxReturn($return, '未登录', 100);
        }
    }

    public function del_message(){
        $uid = $this->uid;
        $message_id = intval($_GET['message_id']);

        if (!$uid) {
            $return[] = '';
            $this->ajaxReturn($return, '未登录', 100);
        }
        if(!$message_id){
            $return[] = '';
            $this->ajaxReturn($return, 'id参数不正确', 100);
        }
        $rs = model('MypushNotice')->where(['id' => $message_id ])->save(['status'=>4]);
        if($rs){
            $return[] = '';
            $this->ajaxReturn($return, 'success', 200);
        }else{
            $this->ajaxReturn([], 'error', 100);
        }
    }

    //我的消息 【关注了我，评论了我】
    public function my_message_v2()
    {
        $limit = isset($_REQUEST['page_size']) && $_REQUEST['page_size'] ?  $_REQUEST['page_size'] : 10;
        $p = isset($_REQUEST['p']) && $_REQUEST['p'] ?  $_REQUEST['p'] : 1;
        $start = ($p - 1) * $limit;
        $uid = $this->uid;
        $return_1 = [];
        $notice_rs_total = 0;
        if ($uid) {
            //我的feed数组
            $notice_rs_total = model('MypushNotice')->where(['to_uid' => $uid , 'status'=> ['in', '1,0']])->count();
            $notice_rs = model('MypushNotice')->where(['to_uid' => $uid])->limit($start .','.$limit)->order('id desc')->select();
            if ($notice_rs) {
                foreach ($notice_rs as $feed) {
                    $feeds_arr[] = $feed['feed_id'];
                }
            }
            if (count($notice_rs) > 0) {
                //$where['feed_id'] = ['in', $feeds_arr];
                //$didd_list = model('FeedDigg')->where($where)->order('id desc')->select();
                if ($notice_rs) {
                    foreach ($notice_rs as $v) {
                        //$feed_data_rs = d('FeedData')->where(['feed_id' => $v['feed_id']])->find();
                        if( $v['push_type'] == 2){
                            //获取评论内容
                            $feed_content = model('Comment')->where( ['comment_id', $v['comment_id']] )->order('comment_id desc')->find();
                        }
                        $user_info = getUserInfo($v['to_uid']);
                        $comment_user_info = getUserInfo($v['uid']); //谁评论的
                        $user_info_['uid'] = $user_info['uid'];
                        $user_info_['uname'] = $user_info['uname'];
                        $user_info_['nickname'] = $user_info['nickname'];
                        $user_info_['headimgurl'] = $user_info['headimgurl'];
                        $comment_user_info_['uid'] = $comment_user_info['uid'];
                        $comment_user_info_['uname'] = $comment_user_info['uname'];
                        $comment_user_info_['nickname'] = $comment_user_info['nickname'];
                        $comment_user_info_['headimgurl'] = $comment_user_info['headimgurl'];
                        $return_4['operator_user_info'] = $comment_user_info_;
                        $return_4['user_info'] = $user_info_;
                        $return_4['feed_title'] = $v['feed_title'];
                        $return_4['feed_id'] = $v['feed_id'];
                        $return_4['message_id'] = $v['id'];
                        $return_4['is_read'] = empty($v['status']) ? 0 : 1;
                        $return_4['comment_content']    = isset($feed_content)&& $feed_content ? $feed_content['content'] : '';
                        $return_4['message_type'] = $v['push_type'];
                        $return_4['addtime'] = date('Y-m-d H:i', $v['addtime']);
                        unset($return_4['user_info']['user_group']) ;
                        unset($return_4['user_info']['api_user_group']) ;
                        unset($return_4['user_info']['group_icon_only']) ;
                        unset($return_4['user_info']['credit_info']) ;
                        $return_1[] = $return_4;
                    }
                }
            }
            $return['total'] = intval($notice_rs_total) ;
            $return['current_page'] = $p ;
            $return['total_page']   = ceil( $notice_rs_total / $limit) ;
            $return['message_list'] = $return_1 ;
            $this->ajaxReturn($return, 'success', 200);
        } else {
            $return[] = '';
            $this->ajaxReturn($return, '未登录', 100);
        }
    }

    //我关注的设计师
    public function my_design()
    {
        $uid = $this->uid;
        $page = isset($_REQUEST['page']) && $_REQUEST['page'] ? $_REQUEST['page'] : 1;
        $page_size = isset($_REQUEST['page_size']) && $_REQUEST['page_size'] ? $_REQUEST['page_size'] : 10;
        $page_start = ($page - 1) * $page_size;
        if ($uid) {
            //我的feed数组
            $designer_list = [];
            $designer_rs = model('UserDesigner')->where(['uid' => $uid, 'status' => 1])->limit($page_start, $page_size)->select();
            if ($designer_rs) {
                foreach ($designer_rs as $k => $designer) {
                    $designer_rs[$k]['designer_name'] = $designer['designer_name']; //数据转换
                    $designer_rs[$k]['time'] = date('Y-m-d H:i:s', $designer['addtime']); //数据转换
                    $designer_rs[$k]['designer_avatar'] = $designer['designer_avatar']; //数据转换
                    $designer_rs[$k]['designer_link'] = $designer['designer_link']; //数据转换
                    $designer_rs[$k]['designer_info'] = $designer['designer_info']; //设计的信息---暂定
                }
            } else {
                $designer_rs = $designer_list;
            }

            $designer_rs_num = model('UserDesigner')->where(['uid' => $uid, 'status' => 1])->count();
            $return['count'] = $designer_rs_num;
            $return['designer_list'] = $designer_rs;
            $this->ajaxReturn($return, '', 200);

        } else {
            $return[] = '';
            $this->ajaxReturn($return, '未登录', 100);
        }
    }


    //我关注的设计师
    public function my_brand()
    {
        $uid = $this->uid;
        $page = isset($_REQUEST['page']) && $_REQUEST['page'] ? $_REQUEST['page'] : 1;
        $page_size = isset($_REQUEST['page_size']) && $_REQUEST['page_size'] ? $_REQUEST['page_size'] : 10;
        $page_start = ($page - 1) * $page_size;
        if ($uid) {
            //我的feed数组
            $brand_list = [];
            $brand_rs = model('UserBrand')->where(['uid' => $uid, 'status' => 1])->limit($page_start, $page_size)->select();
            if ($brand_rs) {
                foreach ($brand_rs as $k => $brand) {
                    $designer_rs[$k]['brand_name'] = $brand['brand_name']; //数据转换
                    $designer_rs[$k]['time'] = date('Y-m-d H:i:s', $brand['addtime']); //数据转换
                    $designer_rs[$k]['brand_logo'] = $brand['brand_logo']; //数据转换
                    $designer_rs[$k]['brand_link'] = $brand['brand_link']; //数据转换
                    $designer_rs[$k]['brand_info'] = $brand['brand_info']; //设计的信息---暂定

                }
            } else {
                $brand_rs = $brand_list;
            }

            $brand_rs_num = model('UserBrand')->where(['uid' => $uid, 'status' => 1])->count();
            $return['count'] = $brand_rs_num;
            $return['brand_list'] = $brand_rs;
            $this->ajaxReturn($return, '', 200);

        } else {
            $return[] = '';
            $this->ajaxReturn($return, '未登录', 100);
        }
    }

    //关注设计师
    public function collect_designer()
    {
        $designer_id = $_GET['designer_id'];//设计师id
        if ($designer_id) {
            $db = D('user_designer')->where(array('uid' => $this->uid, 'designer_id' => $designer_id))->find();
            if ($db && $db['status'] == 1) {
                $this->ajaxReturn([], '已关注该设计师');
            }
            if ($db && $db['status'] == 0) {
                $status['status'] = 1;
                model('UserDesigner')->where(array('id'=>$db['id']))->save($status);
                $db = D('user_designer')->where(array('id'=>$db['id']))->find();
                $this->ajaxReturn($db, 'success', 200);
            }
            $url = $this->api_designer . $designer_id;
            /////获取品牌信息
            $data = $this->http_request($url);
            $data = json_decode($data, true);
            if ($data['data']) {
                $data = $data['data'];
                $saveData['designer_name'] = $data['name'];
                $saveData['designer_avatar'] = $data['headPortrait'];
                $saveData['designer_id'] = $data['id'];
                $saveData['designer_info'] = '';
                $saveData['status'] = 1;
                $saveData['addtime'] = time();
                $saveData['uid'] = $this->uid;
                model('UserDesigner')->add($saveData);
                $this->ajaxReturn($saveData, 'success', 200);
            } else {
                $this->ajaxReturn([], '未找到设计师', 404);
            }
        } else {
            $this->ajaxReturn([], 'error', 100);
        }
    }

    //取消关注设计师
    public function cancel_collect_designer()
    {
        $designer_id = $_GET['designer_id'];//设计师id
        if ($designer_id) {
            $db = D('user_designer')->where(array('uid' => $this->uid, 'designer_id' => $designer_id))->find();
            if (!$db) {
                $this->ajaxReturn([], 'error', '100');
            }
            if ($db && $db['status'] == 0) {
                $this->ajaxReturn([], '已取消关注该设计师');
            }
            if ($db && $db['status'] == 1) {
                $saveData['status'] = 0;
                model('UserDesigner')->where(array('id' => $db['id']))->save($saveData);
                $db = D('user_designer')->where(array('id'=>$db['id']))->find();
                $this->ajaxReturn($db, 'success', 200);
            }
        } else {
            $this->ajaxReturn([], 'error', 100);
        }
    }

    //关注品牌
    public function collect_brand()
    {
        $brand_id = $_GET['brand_id'];//品牌id
        if ($brand_id) {
            $db = D('user_brand')->where(array('uid' => $this->uid, 'brand_id' => $brand_id))->find();

            if ($db) {
                if($db['status']==1)
                {
                    $this->ajaxReturn([], '已关注该品牌');
                }
                if($db['status']==0)
                {
                    $saveData['status'] = 1;
                    model('UserBrand')->where(array('id' => $db['id']))->save($saveData);
                    $db = D('user_brand')->where(array('id' => $db['id']))->find();
                    $this->ajaxReturn($db, 'success', 200);
                }
            }
            $url = $this->api_brand . $brand_id;
            /////获取品牌信息
            $data = $this->http_request($url);
            $data = json_decode($data, true);
            if ($data['data']) {
                $data = $data['data'];
                $saveData['brand_name'] = $data['brandName'];
                $saveData['brand_logo'] = $data['brandLogo'];
                $saveData['brand_id'] = $data['id'];
                $saveData['brand_info'] = $data['brandStory'];
                $saveData['status'] = 1;
                $saveData['addtime'] = time();
                $saveData['uid'] = $this->uid;
                model('UserBrand')->add($saveData);
                $this->ajaxReturn($saveData, 'success', 200);
            } else {
                $this->ajaxReturn([], '未找到品牌', 404);
            }
        } else {
            $this->ajaxReturn([], 'error', 100);
        }
    }

    //取消关注品牌
    public function cancel_collect_brand()
    {
        $brand_id = $_GET['brand_id'];//品牌id
        if ($brand_id) {
            $db = D('UserBrand')->where(array('uid' => $this->uid, 'brand_id' => $brand_id))->find();
            if (!$db) {
                $this->ajaxReturn([], 'error', '100');
            }
            if ($db && $db['status'] == 0) {
                $this->ajaxReturn([], '已取消关注该品牌');
            }
            if ($db && $db['status'] == 1) {
                $saveData['status'] = 0;
                model('UserBrand')->where(array('id' => $db['id']))->save($saveData);
                $db = D('user_brand')->where(array('id'=>$db['id']))->find();
                $this->ajaxReturn($db, 'success', 200);
            }
        } else {
            $this->ajaxReturn([], 'error', 100);
        }
    }


    public function saveUserAvatar()
    {
        $return[] = '';
        $uid = $this->uid;
        if ($uid) {
            $saveData['headimgurl'] = $_POST['headimgurl'];
            foreach ($saveData as $k => $v) {
                if (empty($v) && $k == 'headimgurl') {
                    $this->ajaxReturn($return, 'Name不能为空', 100);
                }
            }
            $saveData['update_time'] = time();
            $res = model('User')->where(['uid' => $uid])->save($saveData);
            if ($res) {
                $res && model('User')->cleanCache($this->uid);
                $this->ajaxReturn($return, 'success', 200);
            } else {
                $this->ajaxReturn($return, 'error', 100);
            }
        } else {
            $this->ajaxReturn($return, '未登录', 100);
        }

    }

    //保存个人信息
    public function saveUserProfile()
    {
        $return[] = '';
        $uid = $this->uid;
        if ($uid) {
            $saveData['sex'] = $_POST['sex'];
            $saveData['birthday'] = $_POST['birthday'];
            $saveData['uname'] = $saveData['nickname'] = $_POST['nickname'];
            $saveData['location'] = $_POST['location'];
            $saveData['headimgurl'] = $_POST['headimgurl'];
            foreach ($saveData as $k => $v) {
                if (empty($v) && $k == 'nickname') {
                    $this->ajaxReturn($return, 'Name不能为空', 100);
                }
            }
            $saveData['update_time'] = time();
            $res = model('User')->where(['uid' => $uid])->save($saveData);
            $saveData['first_letter'] = getShortPinyin($saveData['nickname']);/* 用户首字母 */
            if ($res) {

                //$res = model('User')->where("`uid`={$this->mid}")->save($save);
                $res && model('User')->cleanCache($this->uid);
                $user_feeds = model('Feed')->where('uid=' . $this->uid)->field('feed_id')->findAll();
                if ($user_feeds) {
                    $feed_ids = getSubByKey($user_feeds, 'feed_id');
                    model('Feed')->cleanCache($feed_ids, $this->mid);
                }

                $return[] = '';
                $this->ajaxReturn($return, 'success', 200);
            } else {
                $return[] = '';
                $this->ajaxReturn($return, 'error'  , 100);
            }
        } else {
            $this->ajaxReturn($return, '未登录', 100);
        }

    }


    public function doDenounce()
    {
        $uid = $this->uid;
        if (!$uid) {
            $this->ajaxReturn($_POST, '请登录', 100);
        }

        if(empty($_POST['reason_id'])){
            $this->ajaxReturn($_POST,'请选择举报原因',100);
        }

        $reason_id = explode('|',$_POST['reason_id']);
        foreach($reason_id as $k =>$v){
            if($v==6 && empty($_POST['reason'])){
                $this->ajaxReturn($_POST, '请填写举报原因', 100);
            }
        }

        if (empty($_POST['feed_id'])) {
            $this->ajaxReturn($_POST, '请填写举报帖子', 100);
        }

        if(empty($_POST['source_url'])){
            $this->ajaxReturn($_POST,'请填写举报内容详情页url',100);
        }


        $saveData['uid'] = $uid;
        $saveData['reason_id'] = trim($_POST['reason_id']);
        $saveData['reason'] = trim($_POST['reason']);
        $saveData['aid'] = intval($_POST['feed_id']);
        $saveData['fuid'] = D('feed')->where(array('feed_id'=>$saveData['aid']))->getField('uid');
        $saveData['feed_title'] = D('feed_data')->where(array('feed_id'=>$saveData['aid']))->getField('feed_title');
        $saveData['source_url'] = trim($_POST['source_url']);
        $saveData['topic_id'] = D('feed_data')->where(array('feed_id'=>$saveData['aid']))->getField('topic_id');
        $saveData['state'] = 0;
        $saveData['ctime'] = time();


        $rs = D('denounce')->add($saveData);
        if ($rs) {
            $this->ajaxReturn($saveData, 'success', 200);
        } else {
            $this->ajaxReturn($saveData, 'error', 100);
        }
    }

    /**
     * 评论/回复
     *
     */
    public function doComment()
    {
        $GLOBALS['ts']['mid'] = $this->uid;
        // 返回结果集默认值
        $return = array(
            'status' => 200,
            'data'   => L('PUBLIC_CONCENT_IS_ERROR'),
        );

        //检测用户是否被禁言
        if ($isDisabled = model('DisableUser')->isDisableUser($this->mid, 'post')) {
            echo  json_encode(array(
                'status' => 100,
                'data'   => '您已经被禁言了',
            ));exit;
        }

        // 获取接收数据
        $data['app'] = "w3g";
        $data['table'] = "feed";
        $data['content'] = $_POST['content'];
        $data['app_uid'] = 0; //
        $data['app_row_id'] = 0;
        $data['app_row_table'] = 'feed';
        $data['row_id'] = intval($_POST['row_id']); //分享id
        $data['to_comment_id'] = 0;
        $data['to_uid'] = 0;
        $data['ifShareFeed'] = 0;
        $data['comment_old'] = 0;
        $data['app_detail_summary'] = '';

        $source = model('Source')->getSourceInfo($data['table'], $data['row_id'], false, $data['app']);
        $data['to_uid'] = $uid = $source['uid'];
        if ($this->mid != $uid) {
            $userPrivacy = model('UserPrivacy')->getPrivacy($this->mid, $uid);
            if ($userPrivacy['comment_weibo'] == 1) {
                //exit(json_encode(array('status' => 100, 'data' => L('PUBLIC_CONCENT_TIPES'))));
            }
        }
        $filterContentStatus = filter_words($data['content']);
        if (!$filterContentStatus['status']) {
            exit(json_encode(array('status' => 100, 'data' => $filterContentStatus['data'])));
        }
        $data['content'] = $filterContentStatus['data'];

        // 判断资源是否被删除
        $dao = M($data['table']);
        $idField = $dao->getPk();
        $map[$idField] = intval($data['row_id']);
        $sourceInfo = $dao->where($map)->find();

        if (!$sourceInfo) {
            $return['status'] = 100;
            $return['data'] = '内容已被删除，评论失败';
            exit(json_encode($return, JSON_UNESCAPED_UNICODE));
        }

        //兼容旧方法
        if (empty($data['app_detail_summary'])) {
            $source = model('Source')->getSourceInfo($data['table'], $data['row_id'], false, $data['app']);
            $data['app_detail_summary'] = $source['source_body'];
            $data['app_detail_url'] = $source['source_url'];
            $data['app_uid'] = $source['source_user_info']['uid'];
        } else {
            $data['app_detail_summary'] = $data['app_detail_summary'].'<a class="ico-details" href="'.$data['app_detail_url'].'"></a>';
        }
        // $data['from'] = 'feed';
        // 添加评论操作
        $data['comment_id'] = model('Comment')->addComment($data);
        $msg = model('Comment')->getError();
        if($data['comment_id']){
            $this->doContentLog(  'content' , 2 );
        }
        !$data['comment_id'] && $return['data'] = $msg;
        $data['comment_id']  && $return['data'] = $msg;
        $return['comment'] = $data['comment_id'];
        exit(json_encode($return, JSON_UNESCAPED_UNICODE));
    }

    public function doDigg()
    {
        $feed_id = intval($_GET['feed_id']);
        //$feed_id = ($feed_id == 0) ? intval($_GET['feed_id']) : false;
        if( !$feed_id || empty($feed_id) ){
            exit(json_encode(['status'=> 100,'msg'=>'feed_id参数不正确'],JSON_UNESCAPED_UNICODE));
        }
        if(!$this->mid){
            $res['status'] = 100;
            $res['info'] = model('FeedDigg')->getLastError();
            exit(json_encode($res));
        }
        $this->doContentLog('content' , 4);
        $result = model('FeedDigg')->addDigg($feed_id, $this->mid);
        if ($result) {
            $res['status'] = 200;
            $res['info'] = "成功";
        } else {
            $res['status'] = 100;
            $res['info'] = model('FeedDigg')->getLastError();
        }
        exit(json_encode($res,JSON_UNESCAPED_UNICODE));

    }

    public function delDigg()
    {
        $feed_id = intval($_GET['feed_id']);
        $result = model('FeedDigg')->delDigg($feed_id, $this->mid);
        if ($result) {
            $res['status'] = 200;
            $res['info'] = "删除成功";
        } else {
            $res['status'] = 0;
            $res['info'] = model('FeedDigg')->getLastError();
        }
        exit(json_encode($res,JSON_UNESCAPED_UNICODE));
    }




    //后台内容分析：content_stats 表的统计数据
    public function doContentLog($action = 'content' , $type = 1 , $res = 0, $feed_id =0){
        if($action == 'content'){
            $date = date('Y-m-d',time());
            $rs = d('content_stats')->where(['day'=> $date])->find();
            if(!$rs){
                $data['day'] = $date;
                $data['feed_total'] = 0;
                $data['comment_total'] = 0;
                $data['share_total'] = 0;
                $data['love_total'] = 0;
                $data['view_total'] = 0;
                $res = d('content_stats')->add($data);
            }

            if ($type == 1) {
                $res = d('content_stats')->setInc('feeds_total', array('day' => $date), 1);
            } else if ($type == 2) {
                $res = d('content_stats')->setInc('comment_total', array('day' => $date), 1);
            } else if ($type == 3) {
                $res = d('content_stats')->setInc('share_total', array('day' => $date), 1);
            } else if ($type == 4) {
                $res = d('content_stats')->setInc('love_total', array('day' => $date), 1);
            } else if ($type == 5) {
                $res = d('feed')->setInc('visitor_count', array('feed_id' => $feed_id), 1);
                d('content_stats')->setInc('view_total', array('day' => $date), 1);
                model('Feed')->cleanCache($feed_id);
            }
        }
        return $res;

    }

}
